package com.tjbklx1.thread.chap03.demo01;

import java.util.LinkedList;
import java.util.Queue;

public class RequestQueue {

	private final Queue<Request> queue = new LinkedList<Request>();

	public synchronized Request getRequest() { // 注意同步
		while (queue.peek() == null) {
			try {
				System.out.println(Thread.currentThread().getName() + " : wait() begins,queue = " + queue);
				wait();
				System.out.println(Thread.currentThread().getName() + " : wait() ends,queue = " + queue);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return queue.remove();
	}

	public synchronized void putRequest(Request request) { // 注意同步
		queue.offer(request);
		System.out.println(Thread.currentThread().getName() + " : notifyAll() begins,queue = " + queue);
		notifyAll();
		System.out.println(Thread.currentThread().getName() + " : notifyAll() ends,queue = " + queue);
	}
}
